﻿namespace TestTool.GUI.Utils
{
    using System;
    using System.Collections.Generic;
    using System.ServiceModel;
    using System.ServiceModel.Channels;
    using System.Xml;
    using TestTool.HttpTransport;
    using TestTool.HttpTransport.Interfaces;
    using TestTool.Proxies.Event;
    using TestTool.Tests.Common.CommonUtils;
    using TestTool.Tests.Common.Transport;
    using TestTool.Tests.Definitions.Interfaces;

    internal class EventsServiceProvider : BaseServiceProvider<EventPortTypeClient, EventPortType>
    {
        public EventsServiceProvider(string serviceAddress, int messageTimeout) : base(serviceAddress, messageTimeout)
        {
            base.EnableLogResponse = false;
        }

        public override EventPortTypeClient CreateClient(Binding binding, EndpointAddress address)
        {
            EndpointController controller = new EndpointController(new EndpointAddress(base._serviceAddress));
            WsaController controller2 = new WsaController();
            return new EventPortTypeClient(new HttpBinding(new IChannelController[] { this, controller, controller2, base._credentialsProvider }), address);
        }

        protected void FindTopics(XmlElement element, List<XmlElement> topics)
        {
            if (element.RepresentsTopic())
            {
                topics.Add(element);
            }
            foreach (XmlNode node in element.ChildNodes)
            {
                XmlElement element2 = node as XmlElement;
                if (element2 != null)
                {
                    this.FindTopics(element2, topics);
                }
            }
        }

        public List<EventsTopicInfo> GetTopics()
        {
            TopicSetType topicSet = this.GetTopicSet();
            if (((topicSet == null) || (topicSet.Any == null)) || (topicSet.Any.Length == 0))
            {
                return null;
            }
            List<XmlElement> topics = new List<XmlElement>();
            foreach (XmlElement element in topicSet.Any)
            {
                this.FindTopics(element, topics);
            }
            List<EventsTopicInfo> list2 = new List<EventsTopicInfo>();
            foreach (XmlElement element2 in topics)
            {
                EventsTopicInfo plainInfo = TopicInfo.ConstructTopicInfo(element2).GetPlainInfo();
                list2.Add(plainInfo);
            }
            return list2;
        }

        protected TopicSetType GetTopicSet()
        {
            Action unsecure = null;
            string[] response = null;
            bool fixedTopicSet = false;
            TopicSetType topicSet = null;
            string[] topicExpressionDialect = null;
            string[] messageContentFilterDialect = null;
            string[] producerPropertiesFilterDialect = null;
            string[] messageContentSchemaLocation = null;
            XmlElement[] any = null;
            if (base.Security == Security.None)
            {
                if (unsecure == null)
                {
                    unsecure = delegate {
                        response = this.Client.GetEventProperties(out fixedTopicSet, out topicSet, out topicExpressionDialect, out messageContentFilterDialect, out producerPropertiesFilterDialect, out messageContentSchemaLocation, out any);
                    };
                }
                base.ConstructSecurityTolerantAction(unsecure)();
            }
            else
            {
                response = base.Client.GetEventProperties(out fixedTopicSet, out topicSet, out topicExpressionDialect, out messageContentFilterDialect, out producerPropertiesFilterDialect, out messageContentSchemaLocation, out any);
            }
            return topicSet;
        }
    }
}

